<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\ReviewFeedbackModel;
use common\service\DemandOrderService;
use Throwable;
use Webman\Stomp\RetryAbleConsumer;

/**
 * 检查需求单是否已经全部发布完
 * 是: 自动关闭需求单
 */
class CheckDemandOrderClosedConsumer extends BaseConsumer implements RetryAbleConsumer
{

    // 要消费的队列名
    public $queue = 'check_demand_order_closed';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 任务排期
     * @param $data
     * @throws Throwable
     */
    public function doConsume($data)
    {
        $pf = DemandOrderService::getInstance()->isAllPublished($data['review_feedback_id']);
        if (!$pf) {
            return;
        }
        model(ReviewFeedbackModel::class)->modifyItem(['id' => $data['review_feedback_id'], 'status' => ReviewFeedbackModel::STATUS_FINAL]);
    }

    public function getTtr()
    {
        return 10;
    }

    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
